<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>入住页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <script type="text/javascript" src="/js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">刷新</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12  layui-form-pane" id="search">
            <div class="layui-form-item" id="x-city">

                <!--<div class="layui-input-inline">-->
                    <!--<input type="number" name="liveId" id="liveId" placeholder="入住编号" autocomplete="off"-->
                           <!--class="layui-input" >-->
                <!--</div>-->
                <div class="layui-input-inline">
                    <select name="delFlag" id="delFlag">
                        <option value="">入住状态</option>
                        <option value="0">正入住</option>
                        <option value="1">已退房</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <input class="layui-input" placeholder="入住日期" name="orderDate" id="orderDate"
                           autocomplete="off">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline">
                    <input class="layui-input" placeholder="退房日期" name="leaveDate" id="leaveDate" autocomplete="off">
                </div>
                <button type="button" class="layui-btn" lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
            </div>
        </form>
    </div>
    <input type="hidden" id="roomNumValue"/><!--从父窗传房间号到子窗口-->
    <input type="hidden" id="delFlagValue"/><!--从父窗传住房状态到子窗口-->
    <input type="hidden" id="payStatusValue"/><!--从父窗传支付状态到子窗口-->
        <table class="layui-table" id="c_table" lay-filter="test">
        </table>
        <div class="page">
            <div>
                <a class="prev" href="">&lt;&lt;</a>
                <a class="num" href="">1</a>
                <span class="current">2</span>
                <a class="num" href="">3</a>
                <a class="num" href="">489</a>
                <a class="next" href="">&gt;&gt;</a>
            </div>
        </div>
</div>

<script>
    window.onload = function () {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            //执行一个laydate实例
            laydate.render({
                elem: '#orderDate' //指定元素
            });
            //执行一个laydate实例
            laydate.render({
                elem: '#leaveDate' //指定元素
            });
        });

        //获取表格中指定列的值传递给新修改弹窗
        layui.use(['layer', 'table', 'form', 'jquery'], function () {
            var $ = layui.jquery,
                layer = layui.layer,
                form = layui.form;
            var table = layui.table;

            //渲染表格
            var tableRender = {
                id: 'tableId'
                , elem: '#c_table'
                , height: 400
                , url: '/checkin/list' //数据接口
                , toolbar: '#toolbarDemo'
                , parseData: function (res) {
                    var data = res.data;

                    //处理入住状态，转编号为文字
                    for (var index1 in data) {
                        switch (data[index1]["delFlag"]) {
                            case 0:
                                data[index1]["delFlag"] = "正入住";
                                break;
                            case 1:
                                data[index1]["delFlag"] = "已退房";
                                break;
                        }
                    }
                    //处理支付状态，转编号为文字
                    for (var index2 in data) {
                        switch (data[index2]["payStatus"]) {
                            case 0:
                                data[index2]["payStatus"] = "未支付";
                                break;
                            case 1:
                                data[index2]["payStatus"] = "已支付";
                                break;
                        }
                    }
                    return {
                        "code": res.status, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.total, //解析数据长度
                        "data": data //解析数据列表
                    }
                }
                , page: true //开启分页
                , cols: [
                    [ //表头
                          {type: 'checkbox',width:"4%", fixed: 'left'}
                        , {field: 'liveId', title: '入住编号', width: '10%', sort: true, fixed: 'left', align: 'center'}
                        , {field: 'rdid', title: '房号', width: '8%', align: 'center'}
                        , {field: 'cashPledge', title: '押金', width: '6%', align: 'center'}
                        , {field: 'liveNums', title: '入住人数', width: '10%', align: 'center'}
                        , {field: 'orderDate', title: '入住日期', width: '15%', align: 'center'}
                        , {field: 'leaveDate', title: '离开日期', width: '15%', align: 'center'}
                        , {field: 'remark', title: '备注', width: '9%', align: 'center'}
                        , {field: 'delFlag', title: '住房状态', width: '10%', align: 'center'}
                        , {field: 'payment', title: '支付金额', width: '10%', align: 'center'}
                        , {field: 'payStatus', title: '支付状态', width: '12%', align: 'center'}
                        , {field: 'payEdit', title: '操作', toolbar: '#edit', width: '20%', align: 'center'}
                    ]
                ]
                , done: function (res, curr, count) {
                    table.on('tool(test)', function (obj) {
                        //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                        console.log(obj);
                        var data = obj.data; //获得当前行数据
                        //获取当前房间号码
                        var roomNumValue = data['rdid'];
                        $("#roomNumValue").val(roomNumValue);

                        //处理入住状态，转文字为编号
                        var delflag;
                        switch (data["delFlag"]) {
                            case "正入住":
                                delflag = 0;
                                break;
                            case "已退房":
                                delflag = 1;
                                break;
                        }
                        //传值当前住房状态
                        $("#delFlagValue").val(delflag);

                        //处理支付状态，转文字为编号
                        var payStatusValue;
                        switch (data["payStatus"]) {
                            case "未支付":
                                payStatusValue= 0;
                                break;
                            case "已支付":
                                payStatusValue = 1;
                                break;
                        }
                        //传值当前支付状态
                        $("#payStatusValue").val(payStatusValue);


                        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                        //当要触发修改事件的时候，传递data数据
                        if (layEvent == "edit") {
                            editCheckin('编辑入住信息', '/checkin/checkinEdit', 500, 500, data);
                        } else if (layEvent == 'pay') {
                            payCheckin('编辑支付', '/checkin/checkinPay',400,250,data);
                        } else if (layEvent == 'checkOut') { //退房
                            layer.confirm('确定退房吗？', function (index) {
                                console.log("退房 data:",data);
                                $.post("/checkin/checkOut", {'ids': data.liveId}, function (res) {
                                    if (res.status == 0) {
                                        tableIns.reload();
                                        layer.alert("退房成功", {icon: 6})
                                    } else {
                                        layer.alert("退房失败", {icon: 5})
                                    }
                                });
                                layer.close(index);
                                //向服务端发送删除指令
                            });
                        }
                    });
                }

            };
            //渲染表格
            var tableIns = table.render(tableRender);
            form.on('submit(search)', function (data){
                console.log("data",data.field);
                tableIns.reload({
                    where:data.field
                });
            });

            //编辑入住信息
            function editCheckin(title, url, w, h, data) {
                //data为选中行的房间信息
                if (title == null || title == '') {
                    title = false;
                }
                ;
                if (url == null || url == '') {
                    url = "404.html";
                }
                ;
                if (w == null || w == '') {
                    w = ($(window).width() * 0.8);
                }
                ;
                if (h == null || h == '') {
                    h = ($(window).height() - 50);
                }
                ;
                var index = layer.open({
                    type: 2,
                    area: [w + 'px', h + 'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    title: title,
                    content: url,
                    success: function (layero, index) {
                        //在新窗口保留之前的值，在有值的基础上修改,body为子窗口
                        var body = layer.getChildFrame('body',index);
                        //表格渲染后获取指定行数据
                        body.find("#liveId").val(data["liveId"]);
                        // body.find("#yaodid").val(data["rdid"]);
                        body.find("#rdid").val(data["rdid"]);
                        body.find("#cashPledge").val(data["cashPledge"]);
                        body.find("#liveNums").val(data["liveNums"]);
                        body.find("#orderDate").val(data["orderDate"]);
                        body.find("#leaveDate").val(data["leaveDate"]);
                        body.find("#remark").val(data["remark"]);
                        //body.find("#delFlag").val(data["delFlag"]);
                        body.find("#payment").val(data["payment"]);
                        body.find("#payStatus").val(data["payStatus"]);
                    }
                });
            }

            //编辑支付信息
            function payCheckin(title, url, w, h, data) {
                //data为选中行的房间信息
                if (title == null || title == '') {
                    title = false;
                }
                ;
                if (url == null || url == '') {
                    url = "404.html";
                }
                ;
                if (w == null || w == '') {
                    w = ($(window).width() * 0.8);
                }
                ;
                if (h == null || h == '') {
                    h = ($(window).height() - 50);
                }
                ;
                var index = layer.open({
                    type: 2,
                    area: [w + 'px', h + 'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade: 0.4,
                    title: title,
                    content: url,
                    success: function (layero, index) {
                        //在新窗口保留之前的值，在有值的基础上修改,body为子窗口
                        var body = layer.getChildFrame('body', index);
                        //表格渲染后获取指定行数据
                        body.find("#liveId").val(data["liveId"]);
                        body.find("#payment").val(data["payment"]);
                        //body.find("#payStatus").val(data["payStatus"]);
                    }
                });
            }


            //表格头部工具栏
            table.on("toolbar(test)",function (obj) {
                //console.log(obj);
                var checkStatus = table.checkStatus(obj.config.id);
                if(obj.event=='delAll'){
                    layer.confirm('确定删除吗？',function (index) {
                        var arr=[];
                        console.log(checkStatus);
                        $.each(checkStatus.data, function (idx, obj) {
                            //获得所有id
                            arr.push(obj.liveId);
                        });
                        // 异步操作
                        var result = 0;
                        console.log(arr);
                        $.each(arr,function (index,ele) {
                            $.post("/checkin/remove", {'ids': ele}, function (res) {
                                if (res.status!=0)
                                    result=1;
                            });
                        })
                        if (result == 0) {
                            layer.msg("删除成功", {icon: 6})
                            tableIns.reload();
                        }
                        else {
                            layer.alert("删除失败", {icon: 5})
                        }
                        layer.close(index);
                    })
                }
                else if(obj.event=='addCheckin'){
                    addCheckin("增添入住", 700, 500, "/checkin/checkinAdd");
                }
            });

            //增加入住记录
            function addCheckin(title, w, h, url, roomType) {
                if (title == null || title == '') {
                    title = false;
                }
                ;
                if (url == null || url == '') {
                    url = "404.html";
                }
                ;
                if (w == null || w == '') {
                    w = ($(window).width() * 0.9);
                }
                ;
                if (h == null || h == '') {
                    h = ($(window).height() - 50);
                }
                ;
                layer.open({
                    type: 2,
                    area: [w + "px", h + "px"],
                    fix: false,
                    shade: 0.3,
                    title: title,
                    shadeClose: true,//点击遮罩层关闭
                    content: url,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);

                    },
                    end: function () {
                        //层被销毁后刷新页面
                        location.reload();
                    }

                });
            }
        });
    };

</script>
<script type="text/html" id="edit">
    <a class="layui-btn layui-btn-xs  layui-btn-normal" lay-event="pay">支付</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="checkOut">退房</a>
</script>
<!--头部工具栏-->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-danger" lay-event="delAll"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" lay-event="addCheckin">添加入住</button>
    </div>
</script>
</body>

</html></html>